﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Domain.Model;

using Facade.Manager;

using Presentation.Models;

namespace Presentation.Controllers
{
    public class FormController : Controller
    {
        private FormManager webFormManager;
        private ProjectManager projectManager;

        public FormController()
        {
            webFormManager = FormManager.Instance;
            projectManager = ProjectManager.Instance;
        }
       
        //
        // GET: /Form/
         [Authorize]
        public ActionResult Index()
        {
            return RedirectToAction("All");
        }

        //
        // GET: /Form/All
        [Authorize]
        public ActionResult All()
        {
            var forms = webFormManager.Forms;
            return View(forms);
        }

        //
        // GET: /Form/View
        [Authorize]
        public ActionResult View(int id)
        {
            var form = webFormManager.GetFormById(id);
            return View(form);
        }

        //
        // GET: /Form/Edit
        [Authorize(Roles = "admins")]
        public ActionResult Edit(int id)
        {
            var model = new FormViewModel
            {
                Form = webFormManager.GetFormById(id),
                FormElement = new FormElement()
            };

            return View(model);
        }

        //
        // POST: /Form/AddFormElement
        [Authorize(Roles = "admins")]
        [HttpPost]
        public ActionResult AddFormElement(int formId, string name, string type)
        {
            webFormManager.AddFormElement(formId, name, type);

            return RedirectToAction("Edit", new { id = formId });
        }

        //
        // GET: /Form/DeleteFormElement
        [Authorize(Roles = "admins")]
        public ActionResult DeleteFormElement(int formId, string name)
        {
            webFormManager.DeleteFormElementByName(formId, name);

            return RedirectToAction("Edit", new { id = formId });
        }
    }
}
